package collection;

import java.util.Iterator;

class IterableClass implements Iterable<Integer> {

    private final int n;
    private int count = 0;

    public IterableClass(int n) {
        this.n = n;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            @Override
            public boolean hasNext() {
                return count < n;
            }

            @Override
            public Integer next() {
                return count ++;
            }
        };
    }
}

/**
 * 实现了 {@link Iterable} 的类，可用于For-Each
 */
public class IterableDemo {
    public static void main(String[] args) {
        for (Integer integer : new IterableClass(10)) {
            System.out.println(integer);
        }
    }
}
